package com.tadpole.music.view.activity.study;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.luck.picture.lib.tools.ScreenUtils;
import com.shuyu.waveview.AudioPlayer;
import com.tadpole.music.R;
import com.tadpole.music.bean.study.ErrorSingleQuestionBean;
import com.tadpole.music.bean.study.ExaminationErrorListBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.study.ErrorSingleQuestionIView;
import com.tadpole.music.iView.study.ExaminationErrorIView;
import com.tadpole.music.presenter.study.ErrorSingleQuestionPresenter;
import com.tadpole.music.presenter.study.ExaminationErrorListPresenter;
import com.tadpole.music.presenter.study.ExaminationErrorListStudentPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.utils.largeImage.InputStreamBitmapDecoderFactory;
import com.tadpole.music.utils.largeImage.LargeImageView;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.base.LookBigPicActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.widget.BigImagePopup;
import com.tadpole.music.view.widget.LollipopFixedWebView;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 我的考试错题
 * Created By 孙岩
 * on 2020/10/15
 */
public class ErrorExamActivity extends BaseActivity implements ExaminationErrorIView, ErrorSingleQuestionIView {
    //回退
    private View view_back_icon;
    //标题
    private TextView actionbar_title;
    //imageView
    private LargeImageView ivImage;
    //title
    private LollipopFixedWebView tvTitle;
    private LinearLayout llA;
    private LinearLayout llB;
    private LinearLayout llC;
    private LinearLayout llD;
    private TextView tvA;
    private TextView tvB;
    private TextView tvC;
    private TextView tvD;
    private ImageView ivA;
    private ImageView ivB;
    private ImageView ivC;
    private ImageView ivD;
    private TextView tvAText;
    private TextView tvBText;
    private TextView tvCText;
    private TextView tvDText;
    //正确答案
    private TextView tvAnswer;
    private RelativeLayout rlAnswer;
    //重新做题
    private TextView tvAgain;
    //下一题
    private TextView tvNext;


    //播放text的
    private static AudioPlayer audioText;
    //播放背景的
    private static AudioPlayer audioBg;
    //有音频的选择题 播放12345
    private static MediaPlayer media;
    //我的考试错题presenter
    private ExaminationErrorListPresenter examinationErrorListPresenter;
    //我的学生的考试错题presenter
    private ExaminationErrorListStudentPresenter examinationErrorListStudentPresenter;
    //获取题
    private ErrorSingleQuestionPresenter errorSingleQuestionPresenter;
    //考试场次id
    private int id;
    //1表示自己 2表示学生
    private String flag;
    //学生id
    private String student_id;

    //索引
    int index = 0;
    //选择的答案
    private String error;
    //正确答案
    private int right_answer;
    //列表数据源
    private List<ExaminationErrorListBean.DataBeanX.DataBean> list;
    //题的数据源
    private ErrorSingleQuestionBean.DataBean bean;
    private String image;

    private LinearLayout llMore;

    private LinearLayout llE;
    private ImageView ivE;
    private TextView tvEText;
    private TextView tvE;

    private LinearLayout llF;
    private ImageView ivF;
    private TextView tvFText;
    private TextView tvF;

    private LinearLayout llG;
    private ImageView ivG;
    private TextView tvGText;
    private TextView tvG;

    private LinearLayout llH;
    private ImageView ivH;
    private TextView tvHText;
    private TextView tvH;
    private boolean isSelectOne = false;
    private boolean isSelectTwo = false;
    private boolean isSelectThree = false;
    private boolean isSelectFour = false;
    private boolean isSelectFive = false;
    private boolean isSelectSix = false;
    private boolean isSelectSeven = false;
    private boolean isSelectEight = false;
    private List<String> moreList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_error);
        initViews();
        initListeners();
        id = getIntent().getIntExtra("id", 0);
        flag = getIntent().getStringExtra("flag");
        if (!"1".equals(flag)) {
            if ("2".equals(flag)) {//学生
                student_id = getIntent().getStringExtra("student_id");
            }
        }

        examinationErrorListPresenter = new ExaminationErrorListPresenter();
        examinationErrorListPresenter.attachView(this);

        examinationErrorListStudentPresenter = new ExaminationErrorListStudentPresenter();
        examinationErrorListStudentPresenter.attachView(this);

        if ("1".equals(flag)) {//自己
            examinationErrorListPresenter.getExaminationErrorList(id, false);
        } else if ("2".equals(flag)) {//学生
            examinationErrorListStudentPresenter.getExaminationErrorListStudent(id, student_id, false);
        }

        errorSingleQuestionPresenter = new ErrorSingleQuestionPresenter();
        errorSingleQuestionPresenter.attachView(this);
    }

    //初始化控件
    private void initViews() {
        view_back_icon = findViewById(R.id.view_back_icon);
        actionbar_title = findViewById(R.id.actionbar_title);
        tvTitle = findViewById(R.id.tvTitle);
        ivImage = findViewById(R.id.ivImage);
        actionbar_title.setText("实战训练错题");
        llA = findViewById(R.id.llA);
        llB = findViewById(R.id.llB);
        llC = findViewById(R.id.llC);
        llD = findViewById(R.id.llD);
        tvA = findViewById(R.id.tvA);
        tvB = findViewById(R.id.tvB);
        tvC = findViewById(R.id.tvC);
        tvD = findViewById(R.id.tvD);
        ivA = findViewById(R.id.ivA);
        ivB = findViewById(R.id.ivB);
        ivC = findViewById(R.id.ivC);
        ivD = findViewById(R.id.ivD);
        tvAText = findViewById(R.id.tvAText);
        tvBText = findViewById(R.id.tvBText);
        tvCText = findViewById(R.id.tvCText);
        tvDText = findViewById(R.id.tvDText);
        tvAnswer = findViewById(R.id.tvAnswer);
        rlAnswer = findViewById(R.id.rlAnswer);
        tvAgain = findViewById(R.id.tvAgain);
        tvNext = findViewById(R.id.tvNext);
        tvTitle.setBackgroundColor(0);
        llMore = findViewById(R.id.llMore);
        tvEText = findViewById(R.id.tvEText);
        tvFText = findViewById(R.id.tvFText);
        tvGText = findViewById(R.id.tvGText);
        tvHText = findViewById(R.id.tvHText);
        ivE = findViewById(R.id.ivE);
        ivF = findViewById(R.id.ivF);
        ivG = findViewById(R.id.ivG);
        ivH = findViewById(R.id.ivH);
        tvE = findViewById(R.id.tvE);
        tvF = findViewById(R.id.tvF);
        tvG = findViewById(R.id.tvG);
        tvH = findViewById(R.id.tvH);
        llE = findViewById(R.id.llE);
        llF = findViewById(R.id.llF);
        llG = findViewById(R.id.llG);
        llH = findViewById(R.id.llH);
    }

    //初始化监听器
    private void initListeners() {
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                ErrorExamActivity.this.finish();
            }
        });

        //重新做题
        tvAgain.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    setAllSelect();
                    select();
                    stopPlay();
                    rlAnswer.setVisibility(View.INVISIBLE);
                    if (bean != null) {
                        playText(bean.getTitle_sound(), bean.getBg_sound(), bean.getPlay_times());
                    }
                    tvAgain.setEnabled(false);
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //下一题
        tvNext.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {

                    if (tvNext.getText().toString().trim().equals("下一题")) {
                        stopPlay();
                        if ("1".equals(flag)) {//自己
                            examinationErrorListPresenter.getExaminationErrorList(id, false);
                        } else if ("2".equals(flag)) {//学生
                            examinationErrorListStudentPresenter.getExaminationErrorListStudent(id, student_id, false);
                        }
                        index = index + 1;
                    } else if (tvNext.getText().toString().trim().equals("完成")) {
                        ErrorExamActivity.this.finish();
                    }

                    moreList.clear();
                    isSelectOne = false;
                    isSelectTwo = false;
                    isSelectThree = false;
                    isSelectFour = false;
                    isSelectFive = false;
                    isSelectSix = false;
                    isSelectSeven = false;
                    isSelectEight = false;

                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        llA.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (bean.isIs_multy_answer()) {
                    if (isSelectOne) {
                        isSelectOne = false;
                        tvA.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
                    } else {
                        isSelectOne = true;
                        tvA.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
                    }
                } else {
                    selectA();
                    if (right_answer == 1) {
                        showAnimation("恭喜您,答对了！！");
                    } else {
                        if (right_answer == 2) {
                            showAnimation("答错了，正确答案是：B");
                        } else if (right_answer == 3) {
                            showAnimation("答错了，正确答案是：C");
                        } else if (right_answer == 4) {
                            showAnimation("答错了，正确答案是：D");
                        }
                    }
                }

            }
        });

        llB.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (bean.isIs_multy_answer()) {
                    if (isSelectTwo) {
                        isSelectTwo = false;
                        tvB.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
                    } else {
                        isSelectTwo = true;
                        tvB.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
                    }
                } else {
                    selectB();
                    if (right_answer == 2) {
                        showAnimation("恭喜您,答对了！！");
                    } else {
                        if (right_answer == 1) {
                            showAnimation("答错了，正确答案是：A");
                        } else if (right_answer == 3) {
                            showAnimation("答错了，正确答案是：C");
                        } else if (right_answer == 4) {
                            showAnimation("答错了，正确答案是：D");
                        }
                    }
                }

            }
        });

        llC.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {

                if (bean.isIs_multy_answer()) {
                    if (isSelectThree) {
                        isSelectThree = false;
                        tvC.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
                    } else {
                        isSelectThree = true;
                        tvC.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
                    }
                } else {
                    selectC();
                    if (right_answer == 3) {
                        showAnimation("恭喜您,答对了！！");
                    } else {
                        if (right_answer == 1) {
                            showAnimation("答错了，正确答案是：A");
                        } else if (right_answer == 2) {
                            showAnimation("答错了，正确答案是：B");
                        } else if (right_answer == 4) {
                            showAnimation("答错了，正确答案是：D");
                        }
                    }
                }

            }
        });

        llD.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (bean.isIs_multy_answer()) {
                    if (isSelectFour) {
                        isSelectFour = false;
                        tvD.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
                    } else {
                        isSelectFour = true;
                        tvD.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
                    }
                } else {
                    selectD();
                    if (right_answer == 4) {
                        showAnimation("恭喜您,答对了！！");
                    } else {
                        if (right_answer == 1) {
                            showAnimation("答错了，正确答案是：A");
                        } else if (right_answer == 2) {
                            showAnimation("答错了，正确答案是：B");
                        } else if (right_answer == 3) {
                            showAnimation("答错了，正确答案是：C");
                        }
                    }
                }
            }
        });

        llE.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (bean.isIs_multy_answer()) {
                        if (isSelectFive) {
                            isSelectFive = false;
                            tvE.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
                        } else {
                            isSelectFive = true;
                            tvE.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
                        }
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");

                }
            }
        });

        llF.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (bean.isIs_multy_answer()) {
                        if (isSelectSix) {
                            isSelectSix = false;
                            tvF.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
                        } else {
                            isSelectSix = true;
                            tvF.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
                        }
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");

                }
            }
        });

        llG.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (bean.isIs_multy_answer()) {
                        if (isSelectSeven) {
                            isSelectSeven = false;
                            tvG.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
                        } else {
                            isSelectSeven = true;
                            tvG.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
                        }
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");

                }
            }
        });

        llH.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (bean.isIs_multy_answer()) {
                        if (isSelectEight) {
                            isSelectEight = false;
                            tvH.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
                        } else {
                            isSelectEight = true;
                            tvH.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
                        }
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");

                }
            }
        });

        ivImage.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                ArrayList<String> a = new ArrayList<>();
                a.add(image);
                BigImagePopup bigImagePopup = new BigImagePopup(ErrorExamActivity.this, 0, a);
                bigImagePopup.showPopupWindow();
            }
        });
    }

    @Override
    public void showExaminationErrorList(List<ExaminationErrorListBean.DataBeanX.DataBean> list) {
        this.list = list;
        if (list != null && list.size() != 0) {

            if (list.size() == 1) {
                tvNext.setText("完成");
            } else {
                if (index + 1 == list.size()) {
                    tvNext.setText("完成");
                } else {
                    tvNext.setText("下一题");
                }
            }

            error = list.get(index).getErr_selected();
            errorSingleQuestionPresenter.getErrorQuestion(list.get(index).getId());
        }
    }

    @Override
    public void notifyAdapter() {

    }

    @Override
    public void stopRefresh() {

    }

    @Override
    public void show401() {
        SpUtil.getInstance(this).putString(Constant.TOKEN, "");
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

    @SuppressLint({"UseCompatLoadingForDrawables", "CheckResult"})
    @Override
    public void showQuestion(ErrorSingleQuestionBean.DataBean bean) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        if ("1".equals(flag)) {
            tvAgain.setVisibility(View.VISIBLE);
        } else if ("2".equals(flag)) {
            tvAgain.setVisibility(View.INVISIBLE);
        }

        this.bean = bean;

        if (bean.getTitle_img().equals("")) {
            ivImage.setVisibility(View.GONE);
        } else {
            ivImage.setVisibility(View.VISIBLE);
            image = Constant.IMAGE_HEAD + bean.getTitle_img();
            Glide.with(this).load(Constant.IMAGE_HEAD + bean.getTitle_img()).downloadOnly(new SimpleTarget<File>() {

                @Override
                public void onResourceReady(final File resource, Transition<? super File> transition) {

                    try {
                        //主线程
                        ivImage.setImage(new InputStreamBitmapDecoderFactory(new FileInputStream(resource)));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        String url = UrlConfig.getTitle + "q_type=question&id=" + bean.getId();
        tvTitle.loadUrl(url);

        llA.setVisibility(View.VISIBLE);
        llB.setVisibility(View.VISIBLE);
        llC.setVisibility(View.VISIBLE);
        llD.setVisibility(View.VISIBLE);

        if (bean.getAnswer_type() == 1 && bean.getRight_answer() != -1) {//文字

            if (bean.isIs_multy_answer()) {//多选
                llMore.setVisibility(View.VISIBLE);
                llE.setVisibility(View.VISIBLE);
                llF.setVisibility(View.VISIBLE);
                llG.setVisibility(View.VISIBLE);
                llH.setVisibility(View.VISIBLE);
                if (bean.getAnswer_e().equals("")) {
                    llE.setVisibility(View.GONE);
                }
                if (bean.getAnswer_f().equals("")) {
                    llF.setVisibility(View.GONE);
                }
                if (bean.getAnswer_g().equals("")) {
                    llG.setVisibility(View.GONE);
                }
                if (bean.getAnswer_h().equals("")) {
                    llH.setVisibility(View.GONE);
                }

            } else {
                llMore.setVisibility(View.GONE);
            }
            ivA.setVisibility(View.GONE);
            ivB.setVisibility(View.GONE);
            ivC.setVisibility(View.GONE);
            ivD.setVisibility(View.GONE);
            ivE.setVisibility(View.GONE);
            ivF.setVisibility(View.GONE);
            ivG.setVisibility(View.GONE);
            ivH.setVisibility(View.GONE);

            tvAText.setVisibility(View.VISIBLE);
            tvBText.setVisibility(View.VISIBLE);
            tvCText.setVisibility(View.VISIBLE);
            tvDText.setVisibility(View.VISIBLE);
            tvEText.setVisibility(View.VISIBLE);
            tvFText.setVisibility(View.VISIBLE);
            tvGText.setVisibility(View.VISIBLE);
            tvHText.setVisibility(View.VISIBLE);

            tvAText.setText(bean.getAnswer_a());
            tvBText.setText(bean.getAnswer_b());
            tvCText.setText(bean.getAnswer_c());
            tvDText.setText(bean.getAnswer_d());
            tvEText.setText(bean.getAnswer_e());
            tvFText.setText(bean.getAnswer_f());
            tvGText.setText(bean.getAnswer_g());
            tvHText.setText(bean.getAnswer_h());

        } else if (bean.getAnswer_type() == 2 || bean.getAnswer_type() == 5) {//图片
            if (bean.isIs_multy_answer()) {//多选
                llMore.setVisibility(View.VISIBLE);
                llE.setVisibility(View.VISIBLE);
                llF.setVisibility(View.VISIBLE);
                llG.setVisibility(View.VISIBLE);
                llH.setVisibility(View.VISIBLE);
                if (bean.getAnswer_e().equals("")) {
                    llE.setVisibility(View.GONE);
                }
                if (bean.getAnswer_f().equals("")) {
                    llF.setVisibility(View.GONE);
                }
                if (bean.getAnswer_g().equals("")) {
                    llG.setVisibility(View.GONE);
                }
                if (bean.getAnswer_h().equals("")) {
                    llH.setVisibility(View.GONE);
                }

            } else {
                llMore.setVisibility(View.GONE);
            }

            ivA.setVisibility(View.VISIBLE);
            ivB.setVisibility(View.VISIBLE);
            ivC.setVisibility(View.VISIBLE);
            ivD.setVisibility(View.VISIBLE);
            ivE.setVisibility(View.VISIBLE);
            ivF.setVisibility(View.VISIBLE);
            ivG.setVisibility(View.VISIBLE);
            ivH.setVisibility(View.VISIBLE);

            tvAText.setVisibility(View.GONE);
            tvBText.setVisibility(View.GONE);
            tvCText.setVisibility(View.GONE);
            tvDText.setVisibility(View.GONE);
            tvEText.setVisibility(View.GONE);
            tvFText.setVisibility(View.GONE);
            tvGText.setVisibility(View.GONE);
            tvHText.setVisibility(View.GONE);

            Glide.with(this).load(Constant.IMAGE_HEAD + bean.getAnswer_a()).apply(requestOptions).into(ivA);
            Glide.with(this).load(Constant.IMAGE_HEAD + bean.getAnswer_b()).apply(requestOptions).into(ivB);
            Glide.with(this).load(Constant.IMAGE_HEAD + bean.getAnswer_c()).apply(requestOptions).into(ivC);
            Glide.with(this).load(Constant.IMAGE_HEAD + bean.getAnswer_d()).apply(requestOptions).into(ivD);
            Glide.with(this).load(Constant.IMAGE_HEAD + bean.getAnswer_e()).apply(requestOptions).into(ivE);
            Glide.with(this).load(Constant.IMAGE_HEAD + bean.getAnswer_f()).apply(requestOptions).into(ivF);
            Glide.with(this).load(Constant.IMAGE_HEAD + bean.getAnswer_g()).apply(requestOptions).into(ivG);
            Glide.with(this).load(Constant.IMAGE_HEAD + bean.getAnswer_h()).apply(requestOptions).into(ivH);
            if (bean.getAnswer_c().equals("")) {
                llC.setVisibility(View.GONE);
            }
            if (bean.getAnswer_d().equals("")) {
                llD.setVisibility(View.GONE);
            }
        } else {
            llA.setVisibility(View.GONE);
            llB.setVisibility(View.GONE);
            llC.setVisibility(View.GONE);
            llD.setVisibility(View.GONE);
            llE.setVisibility(View.GONE);
            llF.setVisibility(View.GONE);
            llG.setVisibility(View.GONE);
            llH.setVisibility(View.GONE);
            rlAnswer.setVisibility(View.INVISIBLE);
            tvAgain.setVisibility(View.INVISIBLE);
        }

        switch (error) {
            case "1":
                selectA();
                break;
            case "2":
                selectB();
                break;
            case "3":
                selectC();
                break;
            case "4":
                selectD();
                break;
            case "0":
                selectNo();
                ToastUtils.show("这道题未作答");
                break;
        }

        //不能选
        unSelect();
        tvAgain.setEnabled(true);
        rlAnswer.setVisibility(View.VISIBLE);

        if (bean.isIs_multy_answer()) {
            String a1 = bean.getMulty_right_answer().replace("1", "A");
            String a2 = a1.replace("2", "B");
            String a3 = a2.replace("3", "C");
            String a4 = a3.replace("4", "D");
            String a5 = a4.replace("5", "E");
            String a6 = a5.replace("6", "F");
            String a7 = a6.replace("7", "G");
            String a8 = a7.replace("8", "H");
            tvAnswer.setText("正确答案是: " + a8);

        } else {
            right_answer = bean.getRight_answer();
            if (bean.getRight_answer() == 1) {
                tvAnswer.setText("正确答案是: A");
            } else if (bean.getRight_answer() == 2) {
                tvAnswer.setText("正确答案是: B");
            } else if (bean.getRight_answer() == 3) {
                tvAnswer.setText("正确答案是: C");
            } else if (bean.getRight_answer() == 4) {
                tvAnswer.setText("正确答案是: D");
            } else {
                rlAnswer.setVisibility(View.INVISIBLE);
            }
        }

        playText(bean.getTitle_sound(), bean.getBg_sound(), bean.getPlay_times());
    }

    //播放题干
    @SuppressLint("HandlerLeak")
    private void playText(final String title_sound, final String bg_sound, final int cycler) {
        if (title_sound.equals("")) {
            playBgSound(bg_sound, 0, cycler);
        } else {
            audioText = new AudioPlayer(ErrorExamActivity.this, new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what) {
                        case AudioPlayer.HANDLER_CUR_TIME://更新的时间
                            break;
                        case AudioPlayer.HANDLER_COMPLETE://text播放结束
                            playCount(bg_sound, 0, cycler);
                            break;
                        case AudioPlayer.HANDLER_PREPARED://播放开始
                            break;
                        case AudioPlayer.HANDLER_ERROR://播放错误

                            break;
                    }

                }
            });
            audioText.playUrl(Constant.IMAGE_HEAD + title_sound);
        }
    }

    //播放第几遍
    @SuppressLint("HandlerLeak")
    private void playCount(final String bg_sound, final int count, final int cycler) {
        if (bg_sound.equals("")) {

        } else {
            if (media != null) {
                media.stop();
                media.release();
            }
            media = MediaPlayer.create(ErrorExamActivity.this, rediaCount(count));
            media.start();

            media.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    playBgSound(bg_sound, count + 1, cycler);
                }
            });

        }
    }

    //播放背景
    @SuppressLint("HandlerLeak")
    private void playBgSound(final String bg_sound, final int count, final int cycler) {

        audioBg = new AudioPlayer(ErrorExamActivity.this, new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case AudioPlayer.HANDLER_CUR_TIME://更新的时间
                        break;
                    case AudioPlayer.HANDLER_COMPLETE://播放结束
                        if (cycler == 1) {

                        } else if (cycler == 2) {
                            if (count != cycler) {
                                playCount(bg_sound, 1, cycler);
                            }

                        } else if (cycler == 3) {
                            if (cycler - count == 2) {//波了一遍了
                                playCount(bg_sound, 1, cycler);

                            } else if (cycler - count == 1) {
                                playCount(bg_sound, 1, cycler);
                            }

                        } else if (cycler == 4) {


                        } else if (cycler == 5) {

                        }
                        break;
                    case AudioPlayer.HANDLER_PREPARED://播放开始
                        break;
                    case AudioPlayer.HANDLER_ERROR://播放错误
                        break;
                }

            }
        });
        audioBg.playUrl(Constant.IMAGE_HEAD + bg_sound);
    }

    //第一遍 第二遍
    private int rediaCount(int index) {
        int redia = 0;
        switch (index) {
            case 0:
                redia = R.raw.to1;
                break;
            case 1:
                redia = R.raw.to2;
                break;
        }
        return redia;
    }

    //不能选
    private void unSelect() {
        llA.setEnabled(false);
        llB.setEnabled(false);
        llC.setEnabled(false);
        llD.setEnabled(false);
    }

    //能选
    private void select() {
        llA.setEnabled(true);
        llB.setEnabled(true);
        llC.setEnabled(true);
        llD.setEnabled(true);
    }

    //设置为都没选中
    @SuppressLint("UseCompatLoadingForDrawables")
    private void setAllSelect() {
        tvA.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvB.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvC.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvD.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopPlay();
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopPlay();
    }

    //选中a
    @SuppressLint("UseCompatLoadingForDrawables")
    private void selectNo() {
        tvA.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvB.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvC.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvD.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
    }

    //选中a
    @SuppressLint("UseCompatLoadingForDrawables")
    private void selectA() {
        tvA.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
        tvB.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvC.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvD.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
    }

    //选中b
    @SuppressLint("UseCompatLoadingForDrawables")
    private void selectB() {
        tvA.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvB.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
        tvC.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvD.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
    }

    //选中c
    @SuppressLint("UseCompatLoadingForDrawables")
    private void selectC() {
        tvA.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvB.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvC.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
        tvD.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
    }

    //选中d
    @SuppressLint("UseCompatLoadingForDrawables")
    private void selectD() {
        tvA.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvB.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvC.setBackground(getResources().getDrawable(R.drawable.shape_gray_circular_line));
        tvD.setBackground(getResources().getDrawable(R.drawable.shape_main_circular_solid));
    }

    //动画
    private void showAnimation(String str) {
        rlAnswer.setVisibility(View.VISIBLE);
        tvAnswer.setText(str);
        int screenWidth = ScreenUtils.getScreenWidth(this);//获取屏幕宽度
        Animation translateAnimation = new TranslateAnimation(-800, 0, 0, 0);//设置平移的起点和终点
        translateAnimation.setDuration(1000);//动画持续的时间为10s
        translateAnimation.setFillEnabled(true);//使其可以填充效果从而不回到原地
//        translateAnimation.setFillAfter(true);//不回到起始位置
        rlAnswer.setAnimation(translateAnimation);//给imageView添加的动画效果
        translateAnimation.startNow();//动画开始执行 放在最后即可
    }

    //停止播放
    private void stopPlay() {
        //停止播放
        if (audioBg != null) {
            audioBg.stop();
            audioBg = null;
        }
        if (audioText != null) {
            audioText.stop();
            audioText = null;
        }
    }
}
